import axios from '@/libs/api.request'

export default {
  storeName: 'resourceStore',
  namespaced: true,
  state: {

  },
  mutations: {

  },
  actions: {
    getValidateRule () {
      return {
        clientId: [
          { required: true, message: 'clientId不能为空', trigger: 'blur' }
        ],
        resourceName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    },
    listAll ({ state, commit }, { clientId }) {
      return axios.request({
        url: '/api/OauthResource/all',
        params: { clientId },
        method: 'get'
      })
    },
    listChild ({ state, commit }, { clientId, parentId }) {
      return axios.request({
        url: '/api/OauthResource/child',
        params: {
          clientId,
          parentId
        },
        method: 'get'
      })
    },
    save ({ state, commit }, vo) {
      const requestUrl = (vo.resourceId && vo.resourceId !== '') ? '/api/OauthResource/update' : '/api/OauthResource/add'
      return axios.request({
        url: requestUrl,
        method: 'post',
        data: vo
      })
    },
    delete ({ state, commit }, { clientId, resourceId }) {
      return axios.request({
        url: 'api/OauthResource/delete',
        method: 'post',
        data: {id: resourceId, clientId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    }
  }
}
